<template>
  <div class="ding-box">
    <div class="ding-login">
      <transition name="el-fade-in-linear">
        <div class="login-logo" v-loading="loading">
          <img v-lazy="img" alt="">
          <p>{{$t('login.slogan')}}</p>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { ERR_OK } from '@/api/options/statusCode'
import {getDingTalkValidate} from '@/api/dingTalk'
import logo from '@/commom/default/bpm.jpg'
import DingtalkEnv from 'dingtalk-javascript-env'
import LoginMxin from '@/commom/mixins/loginMixin'
import {getWorkUrl} from '@/api/loadWorkFlows'
import dingtalk from 'dingtalk-javascript-sdk'
export default {
  name: 'DingtalkMsg',
  mixins: [LoginMxin],
  data() {
    return {
      img: logo,
      loading: false
    }
  },
  created() {

  },
  mounted() {
    // 钉钉自动登录
    if (DingtalkEnv.isDingTalk) {
      this.loading = true
      const url = this.baserApi
      this.getDingTalkInfo(url)
    }
  },

  methods: {
    // 权限验证
    getDingTalkMsgValidate(option) {
      return new Promise((resolve, reject) => {
        getDingTalkValidate(option).then((res) => {
          this.loading = false
          if (res.code === ERR_OK) {
            const data = res.data
            console.log(data, '获取权限验证--------')
            if (data.PortalRoot == null) {
              window.localStorage.setItem('H3.PortalRoot', '/Portal')
            } else {
              window.localStorage.setItem('H3.PortalRoot', data.PortalRoot)
            }
            this.getWorkUrl()
          }
          resolve(res)
        }).catch(error => {
          this.loading = false
          reject(error)
        })
      })
    },

    // 打开连接
    getWorkUrl() {
      const options = this.$route.params.WorkItemID
      let that = this
      return new Promise((resolve, reject) => {
        getWorkUrl(options).then(res => {
          if (res.code === ERR_OK) {
            let urls = this.baserApi + res.data
            if (DingtalkEnv.isDingTalk && (DingtalkEnv.isWebAndroid || DingtalkEnv.isWebiOS || DingtalkEnv.platform == "iPad")) {
              dingtalk.ready(function() {
                const dd = dingtalk.apis
                dd.biz.util.openLink({
                  url: urls,
                  onSuccess: function(result) {},
                  onFail: function(err) {
                    console.error(err)
                  }
                })
              })
            } else {
              window.location.href = urls
            }
          }
          that.loadingShow = false
          resolve(res)
        }).catch(error => {
          that.loadingShow = false
          reject(error)
        })
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "../../commom/scss/mixin";
  @import "../../commom/scss/varible";
.ding-box{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
  .ding-login {
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .login-logo {
      padding-bottom: 2rem;
      p {
        font-size: 1rem;
        line-height: 2rem;
        color: $textColor;
        padding: 0 10px;
        text-align: center;
        white-space: normal;
      }
      img {
        width: 6.5rem;
        height: 6.5rem;
      }
    }
  }
</style>
